/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include <stdlib.h>
#include <string.h>

#include "../Hogshead.Common.Functions.h"

namespace Hogshead
{
	namespace Common
	{

		template<typename T> class Array2D
		{
		public:
			/**
			* Default constructor is boooring.  Set dimension lenghts to 0 and internal data to NULL.
			*/
			Array2D()
			{
				_d1 = 0;
				_d2 = 0;
				_data = NULL;
			}



			/**
			* Constructor which accepts two dimension sizes.  Pitches an error in the event that
			* either dimension is less than or equal to 0.
			*/
			Array2D(int dimension1, int dimension2)
			{
				if(dimension1 <= 0 || dimension2 <= 0)
				{
					error("Negative Array dimension passed to Array2D constructor");
				}

				_d1 = dimension1;
				_d2 = dimension2;

				_data = new T*[_d1];

				int row_size = sizeof(T) * _d2;
				for(int i = 0; i < _d1; i++)
				{
					//_data[i] = (T*)malloc(row_size);
					_data[i] = new T[row_size];
				}
			}

			/** 
			* Copy constructor, deletes current data nad sets to other's sizes and assigns data in other to this.
			*/

			Array2D(const Array2D& other)
			{
				_d1 = other._d1;
				_d2 = other._d2;
				
				_data = new T*[_d1];

				int row_size = sizeof(T) * _d2;
				for(int i = 0; i < _d1; i++)
				{
					_data[i] = new T[row_size];
					for(int j = 0; j < _d2; j++)
					{
						_data[i][j] = other._data[i][j];
					}
				}
			}

			~Array2D()
			{
				if(_data != NULL)
				{
					for(int i = 0; i < _d1; i++)
						delete[] _data[i];
					delete[] _data;
				}
			}

			T operator()(int i, int j) const
			{
				if(i >= 0 && i < _d1 && j >= 0 && j < _d2)
					return _data[i][j];
				error("Index out of bounds exception");
				throw;
			}

			T& operator()(int i, int j)
			{
				if(i >= 0 && i < _d1 && j >= 0 && j < _d2)
					return _data[i][j];
				error("Index out of bounds exception");
				throw;
			}

			inline int dim1() const
			{
				return _d1;
			}

			inline int dim2() const
			{
				return _d2;
			}
		private:
			int _d1;
			int _d2;
			T** _data;
		};
	}
}